﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class activityAdmin_UserControl_DdlDatePicker : System.Web.UI.UserControl
{
    protected void Page_Init(object sender, EventArgs e)
    {
        //for (int i = 1; i <= 12; i++)
        //{
        //    ddlStartMonth.Items.Add(new ListItem(i.ToString()));
        //}
        for (int i = 2010; i <= DateTime.Today.Year + 1; i++)
        {
            ddlStartYear.Items.Add(new ListItem(i.ToString()));
            ddlEndYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
        }
        InitialDate();
        if (!IsPostBack)
        {
            ddlStartYear.SelectedValue = DateTime.Today.Year.ToString();
            ddlStartMonth.SelectedValue = DateTime.Now.Month.ToString("00");
            ddlEndYear.SelectedValue = DateTime.Now.Year.ToString();
            ddlEndMonth.SelectedValue = DateTime.Now.Month.ToString("00");
            ddlStartDay.SelectedValue = "1";
            ddlEndDay.SelectedValue = DateTime.Today.Day.ToString("00");
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    #region 初始化選擇日期
    protected void ddlStartYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        InitialDate();
        ddlEndYear.SelectedValue = ddlStartYear.SelectedValue;
        ddlEndMonth.SelectedValue = ddlStartMonth.SelectedValue;
        ddlStartDay.SelectedValue = "01";
        ddlEndDay.SelectedValue = ddlEndDay.Items.Count.ToString("00");
    }
    protected void ddlStartMonth_SelectedIndexChanged(object sender, EventArgs e)
    {
        InitialDate();
        ddlEndYear.SelectedValue = ddlStartYear.SelectedValue;
        ddlEndMonth.SelectedValue = ddlStartMonth.SelectedValue;
        ddlStartDay.SelectedValue = "01";
        ddlEndDay.SelectedValue = ddlEndDay.Items.Count.ToString("00");
    }
    private void InitialDate()
    {
        ddlStartDay.Items.Clear();
        ddlEndDay.Items.Clear();
        int daysInMonth = DateTime.DaysInMonth(int.Parse(ddlStartYear.SelectedValue), int.Parse(ddlStartMonth.SelectedValue));
        for (int i = 1; i <= daysInMonth; i++)
        {
            ddlStartDay.Items.Add(new ListItem(i.ToString("00"), i.ToString("00")));
            ddlEndDay.Items.Add(new ListItem(i.ToString("00"), i.ToString("00")));
        }
    }
    protected void ddlEndMonth_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlEndDay.Items.Clear();
        int daysInMonth = DateTime.DaysInMonth(int.Parse(ddlEndYear.SelectedValue), int.Parse(ddlEndMonth.SelectedValue));
        for (int i = 1; i <= daysInMonth; i++)
        {
            ddlEndDay.Items.Add(new ListItem(i.ToString("00"), i.ToString("00")));
        }
        ddlEndDay.SelectedValue = ddlEndDay.Items.Count.ToString("");
    }

    #endregion

    // 取得開始時間
    public DateTime DateStart
    {
        set { DateStart = value; }
        get { return new DateTime(int.Parse(ddlStartYear.SelectedValue), int.Parse(ddlStartMonth.SelectedValue), int.Parse(ddlStartDay.SelectedValue)); }
    }
    // 取得結束時間
    public DateTime DateEnd
    {
        set { DateEnd = value; }
        get { return new DateTime(int.Parse(ddlEndYear.SelectedValue), int.Parse(ddlEndMonth.SelectedValue), int.Parse(ddlEndDay.SelectedValue)); }
    }
    //設定起始時間
    public void SetDateStart(DateTime date)
    {
        ddlStartYear.SelectedValue = date.Year.ToString();
        ddlStartMonth.SelectedValue = date.Month.ToString("00");
        ddlStartDay.SelectedValue = date.Day.ToString("00");
    }
    //設定結束時間
    public void SetDateEnd(DateTime date)
    {
        ddlEndYear.SelectedValue = date.Year.ToString();
        ddlEndMonth.SelectedValue = date.Month.ToString("00");
        ddlEndDay.SelectedValue = date.Day.ToString("00");
    }
}